import 'package:flutter/cupertino.dart';
import 'package:flutter_wan_android/entity/wan_android_user_entity.dart';
import 'package:flutter_wan_android/manager/storage_manager.dart';

class UserModel extends ChangeNotifier {
  static const String kUser = 'kUser';

  WanAndroidUserEntity _user;

  WanAndroidUserEntity get user => _user;

  bool get hasUser => user != null;


  UserModel(){
    var userMap = StorageManager.localStorage.getItem(kUser);
    _user = userMap != null ? WanAndroidUserEntity().fromJson(userMap) : null;
  }

  saveUser(WanAndroidUserEntity user) {
    _user = user;
    notifyListeners();
    StorageManager.localStorage.setItem(kUser, user);
  }

  /// 清除持久化的用户数据
  clearUser() {
    _user = null;
    notifyListeners();
    StorageManager.localStorage.deleteItem(kUser);
  }
}
